# -*- coding: utf-8 -*-

# https://dcache.iqiyi.com/error_dcache/gaoyun.txt
import os
import time,json
from adminApp import create_backgroud_app, db, redis_store
from adminApp.models import Machine, TaskLog,WarnLogs, HardDisk, DefaultNicFlowData as dnf, BandWidthLog,Idc
from datetime import datetime, timedelta, date
from sqlalchemy import func
from dateutil.relativedelta import relativedelta


n=datetime.now()
print(datetime.strftime(n,'%Y-%m-%d H'))
print(datetime.now().minute)

temp_time = datetime.strptime('2020-12-01', '%Y-%m-%d')
end_time = datetime.strptime('2021-1-1 12:20', '%Y-%m-%d %H:%M')

print(temp_time.year,temp_time.month,temp_time.day,temp_time.hour, temp_time.minute)

print(end_time)
print(end_time.minute)
# conver_d = dict()
# while True:
#     conver_d[temp_time] = 0
#     temp_time += timedelta(minutes=5)
#     if temp_time>=end_time:
#         break
# print(conver_d)


# r = os.system("pip install psutil -i https://mirrors.aliyun.com/pypi/simple/")
# print(r)
# r = os.system("pisp show psutdsfgil")
# print(r)
# print(datetime.now())
# print(datetime.now()+timedelta(seconds=-60))
#
# print(datetime.strptime('2012-12-31', '%Y-%m-%d')+timedelta(days=1))
#
# if __name__ == "__main__":
#     # a='2020-09-23 - 2020-09'
#     # print(a.split(' - '))
#     # print(datetime.strptime('2020-09-23','%Y-%m-%d')+timedelta(days=1))
#     # print(date.today().replace(month=1, day=1))
#     # app=create_app(os.getenv('FLASK_ENV', 'default'))
#     app=create_backgroud_app('development')
#     # app.debug = True
#
#     with app.app_context():
#         idcs = db.session.query(Machine.man_mark, func.count('*').label('c'))\
#             .filter(Machine.is_centos == True).filter(Machine.man_mark!=None).group_by(Machine.man_mark)
#         print(idcs)
#         print([k.machines.count() for k in idcs.all()])
#         # q = db.session.query(Machine.id, Machine.system_uuid).join(Idc,isouter=True)
#         # # q = q.filter(Idc.name.like('%{}%'.format('青浦')))
#         # # if idc_son:
#         # #     q = q.filter(Machine.idc.mark.like('%{}%'.format(idc_son)))
#         # q = q.filter(Machine.hostname.like('%{}%'.format('pve')))
#         # # if mark:
#         # #     q = q.filter(Machine.man_mark.like('%{}%'.format(mark)))
#         # machine_ID="1,2,3,4,5"
#         # # if machine_ID:
#         # aids = machine_ID.split(',')
#         # faids = []
#         # for ad in aids:
#         #     try:
#         #         faids.append(int(ad))
#         #     except:
#         #         pass
#         # if faids:
#         #     q = q.filter(Machine.id.in_(faids))
#         # print(q)
#         # print(q.all())
#         # system_uuid='aabbcc'
#         # # for i in range(10):
#         # #     print(i)
#         # #     redis_store.hset('testhash', 'system_uuid-{}'.format(i), datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
#         # #     time.sleep(1)
#         # data_nine_five_last = {k.decode():v.decode() for k, v in redis_store.hgetall('testhash').items()}
#         # print(data_nine_five_last)
#         #
#         # a = redis_store.lrange('testlist', 0, 99)
#         # print(a)
#         # print(len(a))
#         # #LTRIM key start stop
#         # redis_store.ltrim('testlist', 100, 10000000)
#         # print({k[1]:k[0] for k in db.session.query(Machine.id,Machine.system_uuid).filter(Machine.is_centos)})
#         # t0 = time.time()
#         # db.session.bulk_insert_mappings(
#         #     BandWidthLog,
#         #     [
#         #         dict(nic_rx=i-1, nic_tx=i, nic_time='2020-10-22 13:52:58',machine_id=2) for i in range(10000)
#         #     ]
#         # )
#         # db.session.commit()
#         # print("SQLAlchemy ORM bulk_insert_mappings(): Total time for 10000 records " + str(time.time() - t0) + " secs")
#         # 0.11100625991821289 secs
#         # works_group=db.session.query(WorksModel).
#         # group_by(func.year(WorksModel.created_date),func.month(WorksModel.created_date), func.day(WorksModel.created_date))
#         # func.date_format(cls.create_time, "%Y-%m-%d")
#         # func.date_format(Table.create_time, "%Y-%m-%d %H:%i:%s")
#         # print(db.session.query(Machine).filter(Machine.aqy_device_id!=None).filter(Machine.aqy_device_id!='null'))
#         # res = db.session.query(
#         #     db.session.query(dnf).filter(dnf.id > 8).subquery()
#         # )
#         # print(res)
#         # print(res.all())
#         # datetime.now().strftime()
#         # start = "2020-09-17"
#         # start = start if start else date.today().replace(day=1)
#         # print(start, type(start))
#         # fq = db.session.query(dnf.ten_bt_sent.label('bt_sent'),dnf.ten_bt_recv.label('bt_recv'), dnf.flow_datetime.label('td')).order_by(dnf.flow_datetime)
#         #     # .group_by(func.year(dnf.flow_datetime),func.month(dnf.flow_datetime),func.day(dnf.flow_datetime))
#         # fq = fq.filter(dnf.flow_datetime <= start).order_by(func.date_format(dnf.flow_datetime, "%Y-%m-%d"))
#         # print(fq)
#         # print(fq.all())
#     #     version=1.1
#     #     result_md5='result_md5'
#     #     _add=Machine(system_uuid='sdddddddddddddddd2')
#     #     _add.hard_disks.append(HardDisk(Name='sda'))
#     #     db.session.add(_add)
#     #     db.session.commit()
#     #     # _machine = db.session.query(Machine).filter(Machine.system_uuid == system_uuid).one_or_none()
#     #     print(_add.hard_disks.delete())
#     #     # print(_machine,_machine.system_uuid,_machine.version,_machine.default_nic)
#     #     # print(db.session.query(Machine).filter(Machine.updated!=None).filter(Machine.updated<datetime.now()))
